package mock

import (
	"sync"

	"gitee.com/rw555/fdsmastergo/pkg/keypad"
)

// MockKeypad 模拟按键实现
type MockKeypad struct {
	mu        sync.RWMutex
	keyStates map[keypad.Key]keypad.KeyState
}

// NewMockKeypad 创建模拟按键实例
func NewMockKeypad() *MockKeypad {
	return &MockKeypad{
		keyStates: make(map[keypad.Key]keypad.KeyState),
	}
}

// GetKeyState 实现 Keypad 接口
func (m *MockKeypad) GetKeyState(key keypad.Key) (keypad.KeyState, error) {
	m.mu.RLock()
	defer m.mu.RUnlock()
	if state, exists := m.keyStates[key]; exists {
		return state, nil
	}
	return keypad.KeyReleased, nil
}

// SimulateKeyPress 模拟按键按下
func (m *MockKeypad) SimulateKeyPress(key keypad.Key) {
	m.mu.Lock()
	defer m.mu.Unlock()
	m.keyStates[key] = keypad.KeyPressed
}

// SimulateKeyRelease 模拟按键释放
func (m *MockKeypad) SimulateKeyRelease(key keypad.Key) {
	m.mu.Lock()
	defer m.mu.Unlock()
	m.keyStates[key] = keypad.KeyReleased
}

// Reset 重置所有按键状态
func (m *MockKeypad) Reset() {
	m.mu.Lock()
	defer m.mu.Unlock()
	m.keyStates = make(map[keypad.Key]keypad.KeyState)
}
